<?php

require_once(SITE_MODULES_DIR . '/schoolcontact/SchoolcontactWebModule.php');

class IlccontactWebModule extends SchoolcontactWebModule {
    protected function initializeForPage() {
        if (!$this->isLoggedIn('eip')) {
            $this->redirectToModule('login', 'login', array('authority' => 'eip'));  
        }

        $user = $this->getUser();
        $unit = $user->getAttribute('unit');
        if ($unit <> '教育處') {
            $this->unauthorizedAccess();
        }

        switch($this->page) {
            case 'index':
                $units = array();
                $controller = $this->getUnitsFromEip();
                $items = $controller->items();
                foreach($items as $item) {
                    $unit = array();
                    $unit['title'] = $item['area'];
                    $unit['url'] = $this->buildBreadcrumbURL('unit', array('unit' => $item['area']), true);
                    $units[] = $unit;
                }
                $this->assign('units', $units);
                break;
            case 'search':
                $searchTerms = $this->getArg('filter');
                $this->assign('searchTerms', $searchTerms);

                $schools = array();
                $controller = $this->getSearchFromEip($searchTerms);
                $items = $controller->items();
                foreach($items as $item) {
                    $school = array();
                    $school['title'] = $item['name'];
                    $school['url'] = $this->buildBreadcrumbURL('detail', array('name' => $item['name']), true);
                    $schools[] = $school;
                }
                $this->assign('schools', $schools);
                break;
            case 'unit':
                $unit = $this->getArg('unit', '');

                $schools = array();
                $controller = $this->getUnitsFromEip($unit);
                $items = $controller->items();
                foreach($items as $item) {
                    $school = array();
                    $school['title'] = $item['name'];
                    $school['url'] = $this->buildBreadcrumbURL('detail', array('name' => $item['name']), true);
                    $schools[] = $school;
                }
                $this->assign('schools', $schools);
                break;
            case 'detail':
                $pushButton = false;
                $schoolName = $this->getArg('name', '');

                $personDetails = array();
                $controller = $this->getPersonlistFromEip(urlencode($schoolName), 'schname');
                $items = $controller->items();
                foreach($items as $item) {
                    extract($item);
                    $personDetails[] = array(
                        array('label' => '職稱', 'title' => $teajob),
                        array('label' => '姓名', 'title' => $teaname),
                        array('label' => '學校電話', 'title' => $schphone, 'url' => PhoneFormatter::getPhoneURL($schphone)),
                        array('label' => '個人手機', 'title' => $teamobile, 'url' => PhoneFormatter::getPhoneURL($teamobile)),
                        array('label' => '電子郵件', 'title' => $teaemail, 'url' => $this->buildMailToLink($teaemail)),
                    );
                }

                $this->assign('schoolName', $schoolName);
                $this->assign('pushButton', false);
                $this->assign('personDetails', $personDetails);
                break;
        }
    }
}
